package com.cesc.demo.controller;

import com.cesc.demo.mode.PageResult;
import com.cesc.demo.mode.Product;
import com.cesc.demo.mode.ResponseResutl;
import com.cesc.demo.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import java.util.List;

@RestController
@Api(tags = "商品接口")
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @ApiOperation(value="获取所有商品", notes="")
    @GetMapping("")
    public Flux<Product> getProducts(){
        return productService.getProducts();
    }


    @ApiOperation(value="从redis获取所有商品", notes="")
    @GetMapping("/list")
    public Flux<Product> listProductsFromRedis(){
        return productService.listProductsFromRedis();
    }


    @ApiOperation(value="从数据库查询商品", notes="")
    @GetMapping("/search")
    public Mono<PageResult<Product>> seachProductsFromDb(@ApiParam(value = "商品名") String name,
                                                                 @ApiParam(value = "商品编号")String code,
                                                                 @ApiParam(value = "每页大小") int pageSize, @ApiParam(value = "页码")int page){
        return  productService.findByCondition(name,code,pageSize,page);

    }


    @ApiOperation(value="获取具体某个商品信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "商品ID", defaultValue="100", required = true)
    @GetMapping("/{id}")
    public Mono<Product> getProductById(@PathVariable("id") final String id){
        return this.productService.getProductById(id);
    }

    @ApiOperation(value="从redis获取具体某个商品信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "商品ID", defaultValue="100", required = true)
    @GetMapping("/get/{id}")
    public Mono<Product> getProductByIdFromRedis(@PathVariable("id") final String id){
        return this.productService.getProductByIdFromRedis(id);
    }

    @ApiOperation(value="新增商品", notes="")
    @PostMapping("")
    public Mono<Void> createProduct(@RequestBody final Mono<Product> product){
        return this.productService.createOrUpdatePorduct(product);
    }

    @ApiOperation(value="保存商品信息到redis", notes="")
    @PostMapping("/redis/save")
    public Mono<Boolean> saveProductToRedis(@RequestBody Product product){
        return this.productService.saveToRedis(product);
    }


    @ApiOperation(value="保存商品信息到mysql", notes="")
    @PostMapping("/db/save")
    public Mono<ResponseEntity<ResponseResutl<Product>>> saveProductToDb(@RequestBody Product product){
        Mono<Product> productMono=this.productService.saveToDb(product);
        ResponseResutl<Product> responseResutl=new ResponseResutl<>();
        return productMono.map(returnProduct->{
            responseResutl.setCode("200");
            responseResutl.setDate(returnProduct);
            responseResutl.setMesasge("OK");
            ResponseEntity<ResponseResutl<Product>> responseResutlResponseEntity=new ResponseEntity<>(responseResutl, HttpStatus.OK);
            return responseResutlResponseEntity;
        }).onErrorResume(e->{
            System.out.println("api error"); //记录日志
            System.out.println(e.toString());
            responseResutl.setCode("500");
            responseResutl.setDate(null);
            responseResutl.setMesasge("error");
            ResponseEntity<ResponseResutl<Product>> responseError=new ResponseEntity<>(responseResutl, HttpStatus.INTERNAL_SERVER_ERROR);
            return Mono.just(responseError);
        });

    }

    @ApiOperation(value="删除商品", notes="")
    @ApiImplicitParam(name = "id", value = "商品ID", defaultValue="100", required = true)
    @DeleteMapping("/{id}")
    public Mono<Product> delete(@PathVariable("id") final String id){
        return this.productService.deleteProduct(id);
    }
}
